layui.use(['table', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var func = layui.func;

    var monitoringId =  Feng.getUrlParam("id");

    var isOrganization =  Feng.getUrlParam("isOrganization");

    var isInside =  Feng.getUrlParam("isInside");


    /**
     * 污染物信息表管理
     */
    var TPollutantsFactor = {
        tableId: "tPollutantsFactorTable"
    };

    /**
     * 初始化表格的列
     */
    TPollutantsFactor.initColumn = function () {
        return [[
            {field: 'positions', sort: true, title: '位置'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    TPollutantsFactor.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(TPollutantsFactor.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    TPollutantsFactor.openAddDlg = function () {
        func.open({
            title: '添加位置信息',
            content: Feng.ctxPath + '/tPollutantsFactor/addPositionEdit?monitoringId=' +monitoringId + '&isOrganization=' + isOrganization+ '&isInside=' + isInside,
            tableId: TPollutantsFactor.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    TPollutantsFactor.openEditDlg = function (data) {
        func.open({
            title: '修改污染物信息表',
            content: Feng.ctxPath + '/tPollutantsFactor/edit?id=' + data.id,
            tableId: TPollutantsFactor.tableId
        });
    };


    /**
     * 点击编辑污染物因子
     *
     * @param data 点击按钮时候的行数据
     */
    TPollutantsFactor.openEditPollutantsInfo = function (data) {
         var positionss = data.positions;
        func.open({
            title: '编辑污染物因子',
            content: Feng.ctxPath + '/tPollutantsFactor/positionUpdate?id=' + data.id+ '&isOrganization=' + isOrganization+ '&isInside=' + 2 + '&monitoringId=' + monitoringId,
            tableId: TPollutantsFactor.tableId
        });
    };


    /**
     * 导出excel按钮
     */
    TPollutantsFactor.exportExcel = function () {
        var checkRows = table.checkStatus(TPollutantsFactor.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TPollutantsFactor.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/tPollutantsFactor/delete", function (data) {
                if (data.code == 200) {
                    Feng.success("删除成功!");
                }else{
                    Feng.error("删除失败!" + data.message + "!");
                }
                table.reload(TPollutantsFactor.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TPollutantsFactor.tableId,
        url: Feng.ctxPath + '/tPollutantsFactor/positionEditList?monitoringId=' + monitoringId + '&isOrganization=' + isOrganization + '&isInside=' + isInside,
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: TPollutantsFactor.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TPollutantsFactor.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        TPollutantsFactor.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TPollutantsFactor.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + TPollutantsFactor.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            TPollutantsFactor.openEditDlg(data);
        } else if (layEvent === 'delete') {
            TPollutantsFactor.onDeleteItem(data);
        } else if(layEvent === 'editPollutantsInfo'){
            TPollutantsFactor.openEditPollutantsInfo(data);
        }
    });

    form.on('switch(delFlag)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

         TPollutantsFactor.changeDelFlag(id, checked);
    });


});
